package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public Result selectList(String name, String label,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询角色");
        PageResult<Role> list = roleService.selectList(name, label, page, pageSize);
        return Result.success(list);
    }

    @DeleteMapping("/roles/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除角色,id={}", id);
        roleService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/roles")
    public Result add(@RequestBody Role role) {
        log.info("添加角色");
        roleService.add(role);
        return Result.success();
    }

    @GetMapping("/roles/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询角色,id={}",id);
        Role role =roleService.selectById(id);
        return Result.success(role);
    }


    @PutMapping("/roles")
    public Result update(@RequestBody Role role) {
        log.info("更新角色");
        roleService.update(role);
        return Result.success();
    }

    @GetMapping("/roles/list")
    public Result seleceAll(){
        log.info("查询所有角色");
        List<Role> list = roleService.allList();
        return Result.success(list);
    }
}
